package com.ruoyi.scrap.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DeleteStatus;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.scrap.domain.ScrapApplyInto;
import com.ruoyi.scrap.domain.ScrapApplyOut;
import com.ruoyi.scrap.mapper.ScrapApplyIntoMapper;
import com.ruoyi.scrap.mapper.ScrapApplyOutMapper;
import com.ruoyi.scrap.service.ScrapApplyOutService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScrapApplyOutServiceImpl extends ServiceImpl<ScrapApplyOutMapper, ScrapApplyOut> implements ScrapApplyOutService {

    @Resource
    ScrapApplyIntoMapper intoMapper;

    @Override
    public List<ScrapApplyOut> getScrapApplyOut(ScrapApplyOut out) {
        return baseMapper.getScrapApplyOut(out);
    }

    @Override
    @Transactional
    public boolean save(ScrapApplyOut out, SysUser loginUser) {

        if(ObjectUtils.isEmpty(out.getId())){

            out.setUserId(loginUser.getUserId());
            out.setDeptId(loginUser.getDeptId());
            out.setCreateBy(loginUser.getUserName());
            out.setCreateTime(new Date());
            out.setDelFlag(DeleteStatus.OK.getCode());

            ScrapApplyInto into = new ScrapApplyInto();

            into.setId(out.getIntoId());
            into.setStatus("1");
            into.setUpdateBy(loginUser.getUserName());
            into.setUpdateTime(new Date());

            intoMapper.updateById(into);

        }else{

            ScrapApplyInto into = intoMapper.selectById(out.getIntoId());

            if(ObjectUtils.isEmpty(into)){
                throw new BaseException("保存失败：未查询到入库单");
            }

            out.setApplyId(into.getApplyId());

        }

        out.setUpdateBy(loginUser.getUserName());
        out.setUpdateTime(new Date());

        return baseMapper.insertOrUpdate(out);

    }

    @Override
    @Transactional
    public void remove(Long[] ids, SysUser loginUser) {

        Arrays.asList(ids).stream().forEach(x->{

            ScrapApplyOut applyOut = baseMapper.selectById(x);

            if(ObjectUtils.isEmpty(applyOut)){
                throw new BaseException("删除失败：未查询到要修改的出库单");
            }

            ScrapApplyOut out = new ScrapApplyOut();

            out.setId(x);
            out.setDelFlag(DeleteStatus.DELETED.getCode());
            out.setUpdateBy(loginUser.getUserName());
            out.setUpdateTime(new Date());

            baseMapper.updateById(out);

            long exist = baseMapper.selectCount(Wrappers.lambdaQuery(ScrapApplyOut.class)
                    .eq(ScrapApplyOut::getIntoId,applyOut.getIntoId())
                    .eq(ScrapApplyOut::getDelFlag,DeleteStatus.OK.getCode()));

            if(exist == 0){

                ScrapApplyInto into = new ScrapApplyInto();

                into.setId(applyOut.getIntoId());
                into.setStatus("0");
                into.setUpdateBy(loginUser.getUserName());
                into.setUpdateTime(new Date());

                intoMapper.updateById(into);

            }

        });

    }
}
